package com.aramco.fion.util
{	
	import com.aramco.fion.vo.F4Value;
	import com.aramco.fion.vo.VariableInfo;
	import com.aramco.fion.vo.selection.RangeSelectionVo;
	import com.aramco.fion.vo.selection.SelectionDetailsVo;
	import com.aramco.fion.vo.selection.SelectionVo;
	import com.aramco.fion.vo.selection.SingleMultipleSelectionVo;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.xml.SimpleXMLDecoder;


	public final class SelectionUtil
	{
		
		public static function formatVariablesScreenMetadata(metadata:VariableInfo):SelectionVo
		{
			var SelectionVObject:SelectionVo = new SelectionVo();
			var SelectionDetails:SelectionDetailsVo = new SelectionDetailsVo();
			
			if(metadata != null && metadata.defaultValue.length != 0)
			{
				var selections:Array =  metadata.defaultValue.split(";");
				for each(var value:String in selections)
				{
					if(value.length > 0)
					{
						
						value.lastIndexOf("&") == value.length-1 ? value = value.slice(0,value.length - 1): value = value;
						var values:Array = value.split("&");
						
						
						
						switch(values.length)
						{
							case 3: 
								SelectionDetails.singleMultipleSelection.addItem(new SingleMultipleSelectionVo(metadata.iobjnm,values[2],values[0] == "I" ? true:false));
								break;
							case 4:
								SelectionDetails.rangeVo = new RangeSelectionVo(values[2],values[3],values[0] == "I" ? true:false);
								break;
						}
					
					}
				}
				
				SelectionVObject.key = metadata.techname;
				SelectionVObject.inputName = metadata.label;
				SelectionVObject.value = SelectionDetails;
			}
			
			return SelectionVObject;
		}
		
		public static function findObjectByAttribute(arrayCollection:ArrayCollection, attributeName:String, value:String):Object{
			
			for each (var object:Object in arrayCollection) {
				
				if(object[attributeName]==value){
					
					return object;
					
				}
				
			}
			
			return null;
			
		}
		
		public static function findXmlObjectByAttribute(xml:XMLList, attributeName:String, value:String):Object{
			
			for each (var object:Object in xml) {
				
				if(object[attributeName]==value){
					
					return object;
					
				}
				
			}
			
			return null;
			
		}
		
		public static function removeObjectByAttribute(arrayCollection:ArrayCollection, attributeName:String, value:String):void{
			
			var index:int = 0;
			for each (var object:Object in arrayCollection) {
				
				if(object[attributeName]==value){
					
					arrayCollection.removeItemAt(index);
					
				}
				
				index++;
				
			}
			
		}
						
		
		public static function getVariablesScreenMetadataAviation(data:XML):ArrayCollection
		{
			return null;
		}
		
		public static function getF4Values(data:XML):ArrayCollection
		{
			return null;
		}
		
		public static function getXMLReport(data:XML):BSPReportDataModelOO
		{
			return null;
		}
	}
}
